package trader.orig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Poller implements Runnable {
    private final String id;
    private final boolean isSought;
    
    public Poller(String id, boolean isSought) {
        this.id = id;
        this.isSought = isSought;
    }
    
    public Poller(String id) {
        this(id, false);
    }
    
    
    /**
     * Polls an instrument in the a portfolio. 
     */  
    public void run() {
        URL url = null;
        try {
            url = new URL(Util.BASE_URL + "/scripts/php/data/getdata.php");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return;
        }
        while (true) {
            try {
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                //String data = "action=get_portfolio_content&portfolio_id=" + port;
                String data = "action=data_by_id&id=" + id;
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Cookie", Util.sessionId);
                conn.setDoOutput(true);
                OutputStream out = conn.getOutputStream();
                out.write(data.getBytes());
                out.close();
                String line = "";
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = in.readLine()) != null) {
                    if(isSought) {
                        System.out.println(line);
                    }
                    Object obj = Util.fromJSon(line);
                    if (obj instanceof JSONObject) {
                        Object instrObj = ((JSONObject)obj).get("instruments");
                        if (instrObj instanceof JSONArray) {
                            JSONArray instruments = (JSONArray)instrObj;
                            if (instruments.isEmpty()) {
                                //System.out.println("List #" + id + " is emtpy");
                                continue;
                            }
                            for (Object o : instruments) {
                                JSONObject instrument = (JSONObject)o;
                                OwnedInstrument owned = Util.portfolio.get(id);
                                if (owned == null) {
                                    double ask = 0;
                                    if (instrument.get("ask") instanceof Long) {
                                        ask = (double)(Long)instrument.get("ask");
                                    } else if (instrument.get("ask") instanceof Double) {
                                        ask = (Double)instrument.get("ask");
                                    } 
                                    SoughtInstrument sought = Util.sought.get(id);
                                    sought.setSell(ask);
                                    //{"time":1313048616,"instruments":[{"id":"GL5","bid":46.73,"ask":46.8,"diff":-0.51,"diff_pct":-1.078,"time":"09:44","last_updated":1313048698}]}
                                } else {
                                    double bid = 0;
                                    if (instrument.get("bid") instanceof Long) {
                                        bid = (double)(Long)instrument.get("bid");
                                    } else if (instrument.get("bid") instanceof Double) {
                                        bid = (Double)instrument.get("bid");
                                    } 
                                    owned.setBuy(bid);
                                    //{"time":1313048616,"instruments":[{"id":"GL5","bid":46.73,"ask":46.8,"diff":-0.51,"diff_pct":-1.078,"time":"09:44","last_updated":1313048698}]}
                                }
                            }
                        }
                    }
                }
                in.close();
                
            } catch (IOException e) {
                System.out.println(this.getClass() + "#" + id + " failed");
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }
    }
}
